﻿using System;
using System.Text;
using System.Collections.Generic;

using gKodes.Drawing;

namespace gKodes.Collections
{
    public class PaintSchemeCollection : IEnumerable<PaintScheme>
    {
        private Dictionary<int, IPaint> ForePaints;

        private Dictionary<int, IPaint> BackPaints;

        public PaintSchemeCollection()
        {
            this.ForePaints = new Dictionary<int, IPaint>();
            this.BackPaints = new Dictionary<int, IPaint>();
        }

        public void ChangeForePaint(int index, IPaint item)
        {
            this.ForePaints[index] = item;
        }

        public void ChangeBackPaint(int index, IPaint item)
        {
            this.BackPaints[index] = item;
        }

        public void Change(int index, PaintScheme item)
        {
            this.Change(index, item.Foreground, item.Background);
        }

        public void Change(int index, IPaint fg, IPaint bg)
        {
            this.ForePaints[index] = fg;
            this.BackPaints[index] = bg;
        }

        public void Clear()
        {
            this.ForePaints.Clear();
            this.BackPaints.Clear();
        }

        public bool Contains(int index)
        {
            throw new NotImplementedException();
        }

        public void CopyTo(PaintScheme[] array, int arrayIndex)
        {
            throw new NotImplementedException();
        }

        public int Count
        {
            get { return Math.Max(this.ForePaints.Count, this.BackPaints.Count); }
        }

        public bool RemoveForePaint(int index)
        {
            return this.ForePaints.Remove(index);
        }

        public bool RemoveBackPaint(int index)
        {
            return this.BackPaints.Remove(index);
        }

        public bool Remove(int index)
        {
            return this.ForePaints.Remove(index) | this.BackPaints.Remove(index);
        }

        public IPaint GetForePaint(int index)
        {
            if (!this.ForePaints.ContainsKey(index))
            {
                foreach (int i in this.ForePaints.Keys)
                    if ((i & index) == i)
                        return this.ForePaints[i];
                return this.ForePaints[0];
            }
            return this.ForePaints[index];
        }

        public IPaint GetBackPaint(int index)
        {
            if (!this.BackPaints.ContainsKey(index))
            {
                foreach (int i in this.BackPaints.Keys)
                    if ((i & index) == i && i != 0)
                        return this.BackPaints[i];
                return this.BackPaints[0];
            }
            return this.BackPaints[index];
        }

        public PaintScheme this[int index]
        {
            get
            {
                return new PaintScheme(
                    this.GetForePaint(index), this.GetBackPaint(index));
            }
            set
            {
                this.ForePaints[index] = value.Foreground;
                this.BackPaints[index] = value.Background;
            }
        }

        #region IEnumerable<PaintScheme> method's

        public IEnumerator<PaintScheme> GetEnumerator()
        {
            throw new NotImplementedException();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
